using System;
using System.Collections;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	//CID 0222 //2009.02.04 //Begin
	class XSNViewFormatComboBoxWorker
	{
		public static void  formatComboBox(View view)
		{
			XmlElement root = view.Doc.DocumentElement;
			string xdPrefix = view.XdPrefix;
			StringBuilder filterBuffer = new StringBuilder();
			filterBuffer.Append("//*[@");
			filterBuffer.Append(NamespaceUtil.toFullName(xdPrefix, JetConstant.XD_xctname));
			filterBuffer.Append("='" + JetConstant.XCT_combobox + "']");

			string xslIf = NamespaceUtil.toFullName(view.XslPrefix, "if");

			XmlNodeList comboBoxElementList = DOMUtil.selectNodes(root, filterBuffer.ToString(), 0, view.NsResolver);
			foreach (XmlElement comboBoxElement in comboBoxElementList)
			{
				XmlElement ifElement = DOMUtil.selectANode(comboBoxElement, xslIf, view.NsResolver) as XmlElement;
				if( ifElement != null )
				{
					//CID 0288 //2009.05.21 //Begin
					formatPlaceholder( view, ifElement );
					formatSpan( view, ifElement );
					formatSelect( view, comboBoxElement );
					//CID 0288 //2009.05.21 //End
				}
			}
		}

		//CID 0288 //2009.05.21 //Begin
		private static void formatPlaceholder( View view, XmlElement ifElement )
		{
			XmlElement placeholderSelectElement = DOMUtil.selectANode(ifElement, "select", view.NsResolver) as XmlElement;
			if( placeholderSelectElement != null )
			{
				placeholderSelectElement.SetAttribute( "id", "ComboBoxPlaceholder" );
			}
		}
		//CID 0288 //2009.05.21 //End

		private static void formatSelect( View view, XmlElement comboBoxElement )
		{
			XmlElement selectElement = DOMUtil.selectANode(comboBoxElement, "select", view.NsResolver) as XmlElement;
			if( selectElement != null )
			{
				//CID 0288 //2009.05.21 //Begin
				selectElement.SetAttribute( "ComboBoxSelect", "true" );
				//CID 0288 //2009.05.21 //End
			}
		}

		private static void formatSpan( View view, XmlElement ifElement )
		{
			//CID 0255 //2009.05.19 //"input"
			XmlElement spanElement = DOMUtil.selectANode(ifElement, "input", view.NsResolver) as XmlElement;
			if( spanElement != null )
			{
				//CID 0288 //2009.05.21 //Begin
				spanElement.SetAttribute( "ComboBoxInput", "true" );
				//CID 0288 //2009.05.21 //End
			}
		}
	}
	//CID 0222 //2009.02.04 //End
}